<template>
  <div class="theme-select-container">
    <Button 
      icon="pi pi-palette" 
      class="theme-button"
      @click="toggleThemePanel"
      :aria-label="'主题切换'"
    />
    
    <!-- 主题选择面板 -->
    <div v-if="showThemePanel" class="theme-panel">
      <div class="theme-header">
        <h3>主题设置</h3>
        <Button 
          icon="pi pi-times" 
          class="close-button"
          @click="closeThemePanel"
        />
      </div>
      
      <div class="theme-content">
        <!-- 主题模式切换 -->
        <div class="theme-section">
          <h4>主题模式</h4>
          <div class="theme-mode-options">
            <div 
              class="mode-option"
              :class="{ active: layoutStore.layoutMode === 'light' }"
              @click="setLayoutMode('light')"
            >
              <i class="pi pi-sun"></i>
              <span>浅色模式</span>
            </div>
            <div 
              class="mode-option"
              :class="{ active: layoutStore.layoutMode === 'dark' }"
              @click="setLayoutMode('dark')"
            >
              <i class="pi pi-moon"></i>
              <span>深色模式</span>
            </div>
            <div 
              class="mode-option"
              :class="{ active: layoutStore.layoutMode === 'auto' }"
              @click="setLayoutMode('auto')"
            >
              <i class="pi pi-desktop"></i>
              <span>跟随系统</span>
            </div>
          </div>
        </div>
        
        <!-- 主题颜色选择 -->
        <div class="theme-section">
          <h4>主题颜色</h4>
          <div class="color-options">
            <div 
              v-for="color in themeColors" 
              :key="color.name"
              class="color-option"
              :class="{ active: currentThemeColor === color.value }"
              :style="{ backgroundColor: color.value }"
              @click="setThemeColor(color.value)"
            >
              <i v-if="currentThemeColor === color.value" class="pi pi-check"></i>
            </div>
          </div>
        </div>
        
        <!-- 侧边栏设置 -->
        <div class="theme-section">
          <h4>侧边栏设置</h4>
          <div class="sidebar-options">
            <div class="option-item">
              <span>侧边栏模式</span>
              <div class="switch-options">
                <div 
                  class="switch-option"
                  :class="{ active: layoutStore.sidebarMode === 'vertical' }"
                  @click="setSidebarMode('vertical')"
                >
                  垂直
                </div>
                <div 
                  class="switch-option"
                  :class="{ active: layoutStore.sidebarMode === 'horizontal' }"
                  @click="setSidebarMode('horizontal')"
                >
                  水平
                </div>
              </div>
            </div>
            <div class="option-item">
              <span>侧边栏固定</span>
              <div class="switch-options">
                <div 
                  class="switch-option"
                  :class="{ active: layoutStore.sidebarFixed }"
                  @click="toggleSidebarFixed"
                >
                  {{ layoutStore.sidebarFixed ? '固定' : '不固定' }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <div class="theme-footer">
        <Button 
          label="重置默认" 
          class="reset-button"
          @click="resetTheme"
        />
        <Button 
          label="确定" 
          class="confirm-button"
          @click="confirmTheme"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useLayoutStore } from '../../store/modules/layout'
import Button from 'primevue/button'

// 布局store
const layoutStore = useLayoutStore()

// 主题面板显示状态
const showThemePanel = ref(false)

// 主题颜色选项
const themeColors = [
  { name: '默认蓝', value: '#1890ff' },
  { name: '绿色', value: '#52c41a' },
  { name: '红色', value: '#ff4d4f' },
  { name: '橙色', value: '#fa8c16' },
  { name: '紫色', value: '#722ed1' },
  { name: '青色', value: '#13c2c2' },
  { name: '粉色', value: '#eb2f96' },
  { name: '灰色', value: '#666666' }
]

// 当前主题颜色
const currentThemeColor = computed(() => layoutStore.themeColor)

// 切换主题面板
const toggleThemePanel = () => {
  showThemePanel.value = !showThemePanel.value
}

// 关闭主题面板
const closeThemePanel = () => {
  showThemePanel.value = false
}

// 设置布局模式
const setLayoutMode = (mode: 'light' | 'dark' | 'auto') => {
  layoutStore.setLayoutMode(mode)
}

// 设置主题颜色
const setThemeColor = (color: string) => {
  layoutStore.setThemeColor(color)
}

// 设置侧边栏模式
const setSidebarMode = (mode: 'vertical' | 'horizontal') => {
  layoutStore.setSidebarMode(mode)
}

// 切换侧边栏固定状态
const toggleSidebarFixed = () => {
  layoutStore.setSidebarFixed(!layoutStore.sidebarFixed)
}

// 重置主题
const resetTheme = () => {
  layoutStore.resetTheme()
}

// 确认主题设置
const confirmTheme = () => {
  closeThemePanel()
  // 这里可以保存主题设置到服务器
}

// 点击外部关闭面板
onMounted(() => {
  document.addEventListener('click', (e) => {
    const container = document.querySelector('.theme-select-container')
    if (container && !container.contains(e.target as Node)) {
      showThemePanel.value = false
    }
  })
})
</script>

<style scoped>
.theme-select-container {
  position: relative;
  margin-right: 10px;
}

.theme-button {
  background: transparent;
  border: none;
  color: #333;
  padding: 8px;
}

.theme-panel {
  position: absolute;
  top: 100%;
  right: 0;
  width: 320px;
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  margin-top: 8px;
}

.theme-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e0e0e0;
}

.theme-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-button {
  background: transparent;
  border: none;
  color: #666;
  padding: 4px;
}

.theme-content {
  padding: 16px;
  max-height: 400px;
  overflow-y: auto;
}

.theme-section {
  margin-bottom: 24px;
}

.theme-section h4 {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.theme-mode-options {
  display: flex;
  gap: 8px;
}

.mode-option {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.mode-option:hover {
  border-color: #1890ff;
}

.mode-option.active {
  border-color: #1890ff;
  background-color: #f0f8ff;
  color: #1890ff;
}

.mode-option i {
  font-size: 20px;
  margin-bottom: 4px;
}

.mode-option span {
  font-size: 12px;
}

.color-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.color-option {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid transparent;
  transition: all 0.2s;
}

.color-option:hover {
  transform: scale(1.1);
}

.color-option.active {
  border-color: #333;
  transform: scale(1.1);
}

.color-option i {
  color: white;
  font-size: 16px;
  text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}

.sidebar-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.option-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.option-item span {
  font-size: 14px;
  color: #333;
}

.switch-options {
  display: flex;
  gap: 4px;
}

.switch-option {
  padding: 4px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
}

.switch-option:hover {
  border-color: #1890ff;
}

.switch-option.active {
  background-color: #1890ff;
  color: white;
  border-color: #1890ff;
}

.theme-footer {
  padding: 12px 16px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  gap: 8px;
}

.reset-button,
.confirm-button {
  flex: 1;
  font-size: 12px;
  padding: 6px 12px;
}

.reset-button {
  background: #f0f0f0;
  border: 1px solid #d9d9d9;
  color: #666;
}

.confirm-button {
  background: #1890ff;
  border: 1px solid #1890ff;
  color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .theme-panel {
    width: 280px;
    right: -10px;
  }
  
  .color-options {
    grid-template-columns: repeat(3, 1fr);
  }
}
</style>
